package com.yinhai.message.implement.sms.adapter;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import com.yinhai.message.api.bean.TMUser;
import com.yinhai.message.api.util.MessageType;
import com.yinhai.message.api.vo.TMMessageVo;
import com.yinhai.message.core.adapterManager.IAdapter;
import com.yinhai.message.core.event.IMessageEvent;
import com.yinhai.message.core.exception.ResultsHandle;
import com.yinhai.message.implement.app.adapter.AppAdapter;
import com.yinhai.message.implement.sms.event.SmsMessageEvent;
import com.yinhai.message.implement.sms.sender.SmsMessageSender;

@Component("smsAdapter")
public class SmsAdapter implements IAdapter, ApplicationListener<SmsMessageEvent>{
	private static final Logger LOGGER = LoggerFactory.getLogger(AppAdapter.class);

	@Resource(name = "resultsHandle")
    private ResultsHandle resultsHandle;
	
	@Resource(name = "smsMessageSender")
	private SmsMessageSender smsMessageSender;
	
	@Override
	public String getSenderType() {
		return MessageType.SMS;
	}

	@Override
	public IMessageEvent getEven(TMMessageVo vo) {
		return new SmsMessageEvent(vo);
	}
	
	@Async
	@Override
	public void onApplicationEvent(SmsMessageEvent event) {
		LOGGER.debug("sms处理事件===============================================");
		TMMessageVo messageSendVO = event.getMessageVo();
	    String mgid = messageSendVO.getMessage().getMgid();
	    List<TMUser> resuleUserList = smsMessageSender.sendMessage(messageSendVO);
	    resultsHandle.handleResults(resuleUserList,mgid,getSenderType());
	}
}
